home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / MSJV7_2B.ARJ / STREAM.C < prev    next >
C/C++ Source or Header  |  1992-03-01  |  15KB  |  427 lines

  1. /*
  2.  * stream.c - io stream function callbacks
  3.  *
  4.  * Created by Microsoft Corporation.
  5.  * (c) Copyright Microsoft Corp. 1990 - 1992  All Rights Reserved
  6.  */
  7.  
  8. /***************************************************************************
  9.  * This file contains all routines that directly and indirectly deal with
  10.  * file i/o.  The OLE stream call back functions exist in this file.     
  11.  **************************************************************************/
  12.  
  13. //*** INCLUDES ***
  14.  
  15. #include <windows.h>
  16. #include <ole.h>
  17.  
  18. #include "global.h"
  19. #include "utility.h"
  20. #include "stream.h"
  21. #include "object.h"
  22. #include "demorc.h"
  23.  
  24. //*** Globals ***
  25.  
  26. BOOL fLoadFile = FALSE;
  27.  
  28. /***************************************************************************
  29.  *  lread()
  30.  *
  31.  *  This is function is essentially an _lread() that
  32.  *  can read more than 64K. We need this due to the fact
  33.  *  that objects and files may be greater than 64K.
  34.  *  This function must be declared PASCAL so that it won't
  35.  *  conflict with the _lread() function.
  36.  *
  37.  *  returns DWORD       - number of bytes actually read
  38.  **************************************************************************/
  39.  
  40. DWORD PASCAL lread(                    //* ENTRY:
  41.    HANDLE         hFile,               //* DOS file handle
  42.    void FAR       *pBuffer,            //* buffer to be read into from hfile
  43.    DWORD          dwBytes              //* byte count
  44. ){                                     //* LOCAL:
  45.    BYTE  huge     *hpBuffer = pBuffer; //* buffer containing data read    
  46.    DWORD          dwByteCount = dwBytes;//* number of bytes read 
  47.  
  48.    while (dwByteCount > MAXREAD) 
  49.    {
  50.       if (_lread(hFile, hpBuffer, (WORD)MAXREAD) != MAXREAD)
  51.          return(dwBytes - dwByteCount);
  52.       dwByteCount -= MAXREAD;
  53.       hpBuffer += MAXREAD;
  54.    }
  55.  
  56.  
  57.    if ((dwByteCount -= (DWORD)_lread(hFile, hpBuffer, (WORD)dwByteCount)))
  58.       return(dwBytes - dwByteCount);
  59.  
  60.    return dwBytes;                      //* return
  61. }
  62.  
  63. /***************************************************************************
  64.  *  lwrite()
  65.  *
  66.  *  This function is essentially an _lwrite() which can handle writes 
  67.  *  greater than 64K. This function must be declared PASCAL so that it 
  68.  *  won't conflict with the _lwrite() function.
  69.  *
  70.  *  Returns DWORD       - number of bytes actually written
  71.  **************************************************************************/
  72.  
  73. DWORD PASCAL lwrite(                   //* ENTRY:
  74.    HANDLE         hFile,               //* DOS file handle
  75.    void FAR       *pBuffer,            //* buffer to write to hfile
  76.    DWORD          dwBytes              //* number of bytes
  77. ){                                     //* LOCAL:
  78.    DWORD          dwByteCount = dwBytes;//* number of bytes to write 
  79.    BYTE  huge     *hpBuffer = pBuffer; //* buffer of data to be written
  80.  
  81.    while (dwByteCount > MAXREAD) 
  82.    {
  83.       if (_lwrite(hFile, (LPSTR)hpBuffer, (WORD)MAXREAD) != MAXREAD)
  84.          return(dwBytes - dwByteCount);
  85.       dwByteCount -= MAXREAD;
  86.       hpBuffer += MAXREAD;
  87.    }
  88.  
  89.    if ((dwByteCount -= _lwrite(hFile, (LPSTR)hpBuffer, (WORD)dwByteCount)))
  90.       return(dwBytes - dwByteCount);   //* less bytes than requested read
  91.  
  92.    return dwBytes;                     //* return
  93. }
  94.  
  95. /***************************************************************************
  96.  *  ReadStream() - OLE Callback Function (Get)       
  97.  *
  98.  *  This function is pointed to from the OLESTREAM vtbl; it is Get.
  99.  *  A branch is made based upon whether or not the read request is
  100.  *  greater than 64K.  
  101.  *
  102.  *  returns DWORD  - number of bytes actually read
  103.  **************************************************************************/
  104.  
  105. DWORD FAR PASCAL ReadStream(           //* ENTRY:
  106.    LPAPPSTREAM    lpStream,            //* application stream pointer
  107.    LPSTR          lpstr,               //* string pointer
  108.    DWORD          cb                   //* byte count
  109. ){
  110.  
  111.    if (cb < 0x00010000)
  112.       return _lread(lpStream->fh, lpstr, (WORD) cb);
  113.    else
  114.       return lread(lpStream->fh, lpstr, cb);
  115.  
  116. }
  117.  
  118. /***************************************************************************
  119.  *  WriteStream() - OLE Callback function (Put)
  120.  *
  121.  *  This function is pointed to from the OLESTREAM vtbl; it is Put.
  122.  *  A branch is made based upon wether or not the write request is
  123.  *  greater than 64K.  
  124.  *
  125.  *  Returns DWORD  - number of bytes actually written
  126.  **************************************************************************/
  127.  
  128. DWORD FAR PASCAL WriteStream(          //* ENTRY:
  129.    LPAPPSTREAM    lpStream,            //* application stream pointer 
  130.    LPSTR          lpstr,               //* string pointer
  131.    DWORD          cb                   //* number of bytes to write
  132. ){
  133.  
  134.    if (cb < 0x00010000)
  135.       return _lwrite(lpStream->fh, lpstr, (WORD) cb);
  136.    else
  137.       return lwrite(lpStream->fh, lpstr, cb);
  138.  
  139. }
  140.  
  141. /****************************************************************************
  142.  *  ReadFromFile()
  143.  *
  144.  *  This function reads OLE objects from a file. If the document 
  145.  *  contains manual links, the user will be prompted to update those links.
  146.  *
  147.  *  Returns BOOL  - TRUE if the read(s) were successful
  148.  ***************************************************************************/
  149.  
  150. BOOL FAR ReadFromFile(                 //* ENTRY:
  151.    LPAPPSTREAM    lpStream,            //* application stream pointer
  152.    LHCLIENTDOC    lhcDoc,              //* document handle
  153.    LPOLECLIENT    lpClient             //* pointer to OLE client structure
  154. ){                                     //* LOCAL:
  155.    BOOL           bReturn = FALSE;     //* return value
  156.    unsigned int   cFileObjects;        //* number of file objects
  157.  
  158.    Hourglass(TRUE);
  159.    fLoadFile = TRUE;
  160.  
  161.    _llseek(lpStream->fh, 0L, 0);       //* Read the number of objects 
  162.                                        //* in the file
  163.    if (_lread(lpStream->fh, (LPSTR)&cFileObjects, sizeof(int)) < sizeof(int))
  164.       goto Error;
  165.  
  166.    for (; cFileObjects; --cFileObjects) 
  167.    {
  168.       if (!ObjRead(lpStream,lhcDoc,lpClient)) 
  169.       {
  170.          ErrorMessage(E_FAILED_TO_READ_OBJECT);
  171.          goto Error;
  172.       }
  173.    }
  174.    
  175.    ShowDoc(lhcDoc,1);
  176.    UpdateLinks(lhcDoc);
  177.  
  178.    bReturn = TRUE;                     //* SUCCESS
  179.  
  180. Error:                                 //* ERROR Tag
  181.     
  182.    Hourglass(FALSE);
  183.    fLoadFile = FALSE;
  184.    return bReturn;                     //* return
  185.  
  186. }
  187.  
  188. /****************************************************************************
  189.  *  ObjRead()
  190.  *
  191.  *  Rread an object from the specified file. The file pointer will 
  192.  *  be advanced past the object.
  193.  *
  194.  *  HANDLE fh     - DOS file handle of file to be read from
  195.  *
  196.  *  returns HWND  - window handle to item window containing the OLE object
  197.  ***************************************************************************/
  198.  
  199. BOOL FAR ObjRead(                      //* ENTRY:
  200.    LPAPPSTREAM    lpStream,            //* application stream pointer
  201.    LHCLIENTDOC    lhcDoc,              //* document handle
  202.    LPOLECLIENT    lpClient             //* pointer to OLE client structure
  203. ){                                     //* LOCAL:
  204.    APPITEMPTR     pItem;               //* application item pointer
  205.    LPOLEOBJECT    lpObject;            //* pointer ole object 
  206.    long           otObject;            //* type of object 
  207.    RECT           rcObject;            //* object rect 
  208.    char           szTmp[CBOBJNAMEMAX]; //* temporary string buffer
  209.    char           szProto[PROTOCOL_STRLEN+1];//* protocol string
  210.    int            i;                   //* index
  211.  
  212.    if (_lread(lpStream->fh, szTmp, CBOBJNAMEMAX) < CBOBJNAMEMAX )
  213.       return FALSE;
  214.  
  215.    if (_lread(lpStream->fh, szProto, PROTOCOL_STRLEN) < PROTOCOL_STRLEN )
  216.       return FALSE;
  217.  
  218.    for (i=0; szProto[i] != ' '; i++);
  219.    szProto[i] = NULL;
  220.  
  221.    ValidateName( szTmp );
  222.  
  223.    if (!(pItem = PreItemCreate(lpClient, TRUE, lhcDoc))) 
  224.       return FALSE;
  225.  
  226.    if (Error(OleLoadFromStream((LPOLESTREAM)&(lpStream->olestream), 
  227.          szProto,(LPOLECLIENT)&(pItem->oleclient), lhcDoc, szTmp, &lpObject))) 
  228.       goto Error;
  229.  
  230.    if (_lread(lpStream->fh, (LPSTR)&rcObject, sizeof(RECT)) < sizeof(RECT))
  231.       goto Error;
  232.    
  233.    if (_lread(lpStream->fh, (LPSTR)&otObject, sizeof(long)) < sizeof(long))
  234.       goto Error;
  235.  
  236.    if (PostItemCreate(lpObject, otObject, &rcObject, pItem))
  237.    {
  238.       pItem->fNew = TRUE;
  239.       ObjSetBounds(pItem);
  240.       return TRUE;                     //* SUCCESS return
  241.    }
  242.    else
  243.       return FALSE;
  244.  
  245. Error:                                 //* ERROR Tag
  246.  
  247.    FreeAppItem(pItem);
  248.    return FALSE;
  249.  
  250. }
  251.  
  252. /*************************************************************************
  253.  *  WriteToFile()
  254.  *
  255.  *  Write current document to a file.
  256.  *
  257.  *  returns BOOL - TRUE if file successfully written
  258.  ************************************************************************/
  259.  
  260. BOOL FAR WriteToFile(                  //* ENTRY:
  261.    LPAPPSTREAM    lpStream             //* application stream pointer
  262. ){                                     //* LOCAL:
  263.    int            iObjectsWritten=0;   //* counter of objects written to file
  264.    APPITEMPTR     pItem;               //* application Item pointer
  265.    
  266.    UpdateFromOpenServers();
  267.       
  268.    _llseek(lpStream->fh, 0L, 0);
  269.    
  270.    Hourglass(TRUE);
  271.  
  272.    if (_lwrite(lpStream->fh, (LPSTR)&iObjects, sizeof(int)) < sizeof(int))
  273.       goto Error;
  274.  
  275.    for (pItem = GetTopItem(); pItem; pItem = GetNextItem(pItem))
  276.    {
  277.       if (!ObjWrite(lpStream, pItem)) 
  278.          goto Error;
  279.       iObjectsWritten++;
  280.    }
  281.  
  282.    if (iObjectsWritten != iObjects) 
  283.       goto Error;
  284.  
  285.  
  286.    Dirty(DOC_CLEAN);
  287.    Hourglass(FALSE);
  288.    return(TRUE);                       //* SUCCESS return
  289.  
  290. Error:                                 //* ERROR Tag
  291.     
  292.    Hourglass(FALSE);
  293.    return(FALSE);                      //* ERROR return
  294.  
  295. }
  296.  
  297. /****************************************************************************
  298.  *  ObjWrite()
  299.  *
  300.  *  This function writes an object to the specified
  301.  *  file. The file pointer will be advanced past the end of
  302.  *  the written object.
  303.  
  304.  *  Returns BOOL - TRUE if object written successfully
  305.  ***************************************************************************/
  306.  
  307. BOOL FAR ObjWrite(                     //* ENTRY:
  308.    LPAPPSTREAM    lpStream,            //* application stream pointer
  309.    APPITEMPTR     pItem                //* application item pointer
  310. ){                                     //* LOCAL:
  311.   POINT           pt;                  //* center of rec point
  312.   RECT            rc;                  //* bounding rectangle
  313.   int             cbTmp;
  314.   char            szTmp[PROTOCOL_STRLEN];//* protocol string
  315.  
  316.    cbTmp = CBOBJNAMEMAX;
  317.    OleQueryName(pItem->lpObject, szTmp, &cbTmp);
  318.  
  319.    if (_lwrite(lpStream->fh, szTmp, CBOBJNAMEMAX) < CBOBJNAMEMAX )
  320.       return FALSE;
  321.  
  322.    if (pItem->otObject == OT_STATIC)
  323.       wsprintf(szTmp, "%-15s", STATICP);
  324.    else   
  325.       wsprintf(szTmp, "%-15s", STDFILEEDITING);
  326.  
  327.    if (_lwrite(lpStream->fh, szTmp, PROTOCOL_STRLEN) < PROTOCOL_STRLEN )
  328.       return FALSE;
  329.  
  330.    if (Error(OleSaveToStream(pItem->lpObject, (LPOLESTREAM)&(lpStream->olestream))))
  331.       return FALSE;
  332.  
  333.    GetClientRect(pItem->hwnd, (LPRECT)&rc);
  334.    pt = *(LPPOINT)&rc;
  335.    ClientToScreen(pItem->hwnd, (LPPOINT)&pt);
  336.    ScreenToClient(hwndFrame, (LPPOINT)&pt);
  337.    OffsetRect(
  338.       &rc, 
  339.       pt.x - rc.left - GetSystemMetrics(SM_CXFRAME),
  340.       pt.y - rc.top  - GetSystemMetrics(SM_CYFRAME) 
  341.    );
  342.  
  343.    if (_lwrite(lpStream->fh, (LPSTR)&rc, sizeof(RECT)) < sizeof(RECT)
  344.          || _lwrite(lpStream->fh, (LPSTR)&(pItem->otObject), sizeof(long)) < sizeof(long))
  345.       return FALSE;
  346.  
  347.    return TRUE;                        //* SUCCESS return
  348.  
  349. }
  350.  
  351. /****************************************************************************
  352.  * UpdateLinks()
  353.  *
  354.  * Get the most up to date rendering information and show it.  
  355.  ***************************************************************************/
  356.  
  357. static void UpdateLinks(               //* ENTRY
  358.    LHCLIENTDOC    lhcDoc               //* client document handle
  359. ){                                     //* LOCAL:
  360.    int            i=0;                 //* index
  361.    APPITEMPTR     pItem;               //* temporary item pointer
  362.    char           szUpdate[CBMESSAGEMAX];//* update message?
  363.  
  364.    for (pItem = GetTopItem(); pItem; pItem = GetNextItem(pItem))
  365.    {
  366.       if (pItem->lhcDoc == lhcDoc && pItem->otObject == OT_LINK)
  367.       {   
  368.          if (!i)
  369.          {
  370.             LoadString(hInst, IDS_UPDATELINKS, szUpdate, CBMESSAGEMAX);
  371.             if (MessageBox(hwndFrame, szUpdate, szAppName,
  372.                MB_YESNO | MB_ICONEXCLAMATION) != IDYES)
  373.                break; 
  374.             i++;
  375.          }
  376.          Error(OleUpdate(pItem->lpObject));
  377.       }
  378.    }
  379.  
  380.    WaitForAllObjects();
  381.  
  382. }
  383.  
  384. /****************************************************************************
  385.  * UpdateFromOpenServers()
  386.  *
  387.  * Get the most up to date rendering information before storing it.  
  388.  ***************************************************************************/
  389.  
  390. static void UpdateFromOpenServers(void)
  391. {                                      //* LOCAL:
  392.    APPITEMPTR pItem;                   //* temporary item pointer
  393.    APPITEMPTR pItemNext;
  394.  
  395.    for (pItem = GetTopItem(); pItem; pItem = pItemNext) 
  396.    {
  397.       pItemNext = GetNextItem(pItem); 
  398.       if (pItem->otObject == OT_EMBEDDED || 
  399.          (pItem->uoObject == oleupdate_oncall 
  400.                && pItem->otObject == OT_LINK ))  
  401.  
  402.          if (OleQueryOpen(pItem->lpObject) == OLE_OK)
  403.          {  
  404.             char szMessage[2*CBMESSAGEMAX];
  405.             char szBuffer[CBMESSAGEMAX];
  406.             int cb = CBOBJNAMEMAX;     //* The name will be the server window title.
  407.             char szTmp[CBOBJNAMEMAX];  //* when the object is edited. 
  408.  
  409.             Error(OleQueryName(pItem->lpObject,szTmp,&cb));
  410.             LoadString(hInst, IDS_UPDATE_OBJ, szBuffer, CBMESSAGEMAX);
  411.             wsprintf(szMessage, szBuffer, (LPSTR)szTmp);
  412.  
  413.             if (MessageBox(hwndFrame, szMessage, szAppName, MB_YESNO | MB_ICONEXCLAMATION) == IDYES) 
  414.             {
  415.                Error(OleUpdate(pItem->lpObject));
  416.                WaitForObject(pItem);
  417.             }
  418.             if (!pItem->fVisible)
  419.                ObjDelete(pItem, DELETE);
  420.          }
  421.  
  422.    }
  423.  
  424.    WaitForAllObjects();
  425.  
  426. }
  427.